package com.bupt.controller;

import com.bupt.bean.Device;
import com.bupt.bean.Menu;
import com.bupt.bean.Role;
import com.bupt.bean.User;
import com.bupt.config.*;
import com.bupt.ext.UserExt;
import com.bupt.service.PageService;
import com.bupt.service.UserService;
import com.bupt.vo.RoleConst;
import com.bupt.vo.SysConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 **/
@Api(value="用户信息接口",description="用户权限信息、用户信息的增删改查、角色信息的增删改查、所有功能点查询、单个角色功能点查询、重置用户密码、修改用户密码")
@RestController
@RequestMapping("/ucenter")
public class UcenterController  {
    @Autowired
    UserService userService;

    @Autowired
    PageService pageService;


    @ApiOperation("用户信息及权限")
    @GetMapping("/getuserext")
    public UserExt getUserext(@RequestParam("username") String username) {
        return userService.getUserExt(username);
    }
    @ApiOperation("所有功能点")
    @GetMapping("/menus")
    public List<Menu> getAllMenus() {
        return userService.getAllMenus();
    }


    @ApiOperation("分页查询用户信息页面列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "path", dataType = "int"), @ApiImplicitParam(name = "size", value = "每页记录数", required = true, paramType = "path", dataType = "int")})
    @GetMapping("/userPage/{page}/{size}")
    public QueryResponseResult userPage(@PathVariable("page") int page, @PathVariable("size") int size, QueryPageRequest queryPageRequest) {
        return pageService.finduserList(page, size, queryPageRequest);
    }

    @ApiOperation("用户添加页面")
    @ApiImplicitParams({@ApiImplicitParam(name="userName",value="登录账号",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="passWord",value="账号密码",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="name",value="用户姓名",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="sex",value="性别:1为男性，2为女性",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="email",value="邮箱",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="phone",value="电话号码",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="roleId",value="角色ID",required=true,paramType="query",dataType="string")})
    @PostMapping("/user/add")
    public UserResult add(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord, @RequestParam("name") String name, @RequestParam("sex") String sex, @RequestParam("email") String email, @RequestParam("phone") String phone, @RequestParam("roleId") String roleId) {
        return userService.add(userName, passWord, name, sex,email,phone,roleId);
    }

    @ApiOperation("通过ID修改用户信息")
    @PutMapping("/user/edit/{userId}")
    public UserResult editUserById(@PathVariable("userId") String userId, @RequestParam("userName") String userName, @RequestParam("name") String name, @RequestParam("sex") String sex, @RequestParam("email") String email, @RequestParam("phone") String phone, @RequestParam("roleId") String roleId) {
        return userService.editUserById(userId,userName,name,sex,email,phone,roleId);
    }


    @ApiOperation("删除用户信息")
    @DeleteMapping("/user/deleteById/{userId}")
    public Result deleteById(@PathVariable("userId") String userId) {
        return userService.deleteById(userId);
    }



    @ApiOperation("角色添加页面")
    @ApiImplicitParams({@ApiImplicitParam(name="roleName",value="角色名称",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="roleCode",value="角色代码",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="description",value="描述",required=true,paramType="query",dataType="string")})
    @PostMapping("/role/add")
    public RoleResult addRole(@RequestParam("roleName") String roleName, @RequestParam("roleCode") String roleCode, @RequestParam("description") String description) {
        return userService.addRole(roleName, roleCode, description);
    }
//    @ApiOperation("角色添加页面")
//    @ApiImplicitParams({@ApiImplicitParam(name="roleName",value="角色名称",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="roleCode",value="角色代码",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="description",value="描述",required=true,paramType="query",dataType="string"),@ApiImplicitParam(name="functions",value="功能点,以,分隔(例如1,2,3,4,5)",required=true,paramType="query",dataType="string")})
//    @PostMapping("/role/add")
//    public RoleResult addRole(@RequestParam("roleName") String roleName, @RequestParam("roleCode") String roleCode, @RequestParam("description") String description, @RequestParam("functions") String functions) {
//        return userService.addRole(roleName, roleCode, description, functions);
//    }


    @ApiOperation("分页查询角色信息页面列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "path", dataType = "int"), @ApiImplicitParam(name = "size", value = "每页记录数", required = true, paramType = "path", dataType = "int")})
    @GetMapping("/rolePage/{page}/{size}")
    public QueryResponseResult rolePage(@PathVariable("page") int page, @PathVariable("size") int size, QueryPageRequest queryPageRequest) {
        return pageService.findroleList(page, size, queryPageRequest);
    }

        @ApiOperation("通过角色ID查询功能点")
    @GetMapping("/getMenusByRoleId")
    public List<Menu> getMenusByRoleId(@RequestParam("roleId") String roleId) {
        return userService.getMenusByRoleId(roleId);
    }

//    @ApiOperation("通过ID修改角色信息")
//    @ApiImplicitParams({@ApiImplicitParam(name="functions",value="功能点,以,分隔(例如1,2,3,4,5)",required=true,paramType="query",dataType="string")})
//    @PutMapping("/role/edit/{roleId}")
//    public RoleResult editRoleById(@PathVariable("roleId") String roleId, @RequestParam("roleName") String roleName, @RequestParam("roleCode") String roleCode, @RequestParam("description") String description, @RequestParam("functions") String functions) {
//        return userService.editRoleById(roleId,roleName,roleCode,description,functions);
//    }
    @ApiOperation("通过ID修改角色信息")
    @PutMapping("/role/edit/{roleId}")
    public RoleResult editRoleById(@PathVariable("roleId") String roleId, @RequestParam("roleName") String roleName, @RequestParam("roleCode") String roleCode, @RequestParam("description") String description) {
        return userService.editRoleById(roleId,roleName,roleCode,description);
    }

    @ApiOperation("通过ID修改角色权限")
    @ApiImplicitParams({@ApiImplicitParam(name="functions",value="功能点,以,分隔(例如1,2,3,4,5)",required=true,paramType="query",dataType="string")})
    @PutMapping("/role/editFunctions/{roleId}")
    public RoleResult editFunctions(@PathVariable("roleId") String roleId,@RequestParam("functions") String functions) {
        return userService.editFunctions(roleId,functions);
    }

    @ApiOperation("删除角色信息")
    @DeleteMapping("/role/deleteById/{roleId}")
    public Result deleteByRoleId(@PathVariable("roleId") String roleId) {
        return userService.deleteByRoleId(roleId);
    }

    @ApiOperation("查询所有角色")
    @GetMapping("/role/allRole")
    public List<RoleConst> allRole() {
        return userService.allRole();

    }

    @ApiOperation("重置用户密码,重置后密码为123")
    @PutMapping("/user/resetPassword/{userId}")
    public Result resetPassword(@PathVariable("userId") String userId) {
        return userService.resetpassword(userId);
    }

    @ApiOperation("修改用户密码")
    @PutMapping("/user/updatePassword/{userId}/{beforPassword}/{newPassword}")
    public Result updatePassword(@PathVariable("userId") String userId,@PathVariable("beforPassword") String beforPassword,@PathVariable("newPassword") String newPassword) {
        return userService.updatePassword(userId,beforPassword,newPassword);
    }
}
